//
//  RTOriginalView.swift
//  Weibo
//
//  Created by Brisinga on 16/3/7.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let kIconHW = 35
class RTOriginalView: UIView {
    
    
    
    // 底部约束
    var bottomConstraint: Constraint?
    
    var statusViewModel: RTStatusViewModel? {
    
        didSet {
        
            if let urlString = statusViewModel?.status?.user?.profile_image_url, let url = NSURL(string: urlString) {
            
                icon.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default"))
            }
            
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            sourceLabel.text = statusViewModel?.status?.source
            timeLabel.text = statusViewModel?.status?.created_at
            textLabel.text = statusViewModel?.status?.text
            membershipImage.image = statusViewModel?.membershipImage
            verifiedImage.image = statusViewModel?.verifiedImage
            
            
            // 移除底部约束
            self.bottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
            
                
                pickerView.picUrls = picUrls
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(pickerView).offset(kMargin).constraint
                })
                
                pickerView.hidden = false
                
                
            }else {
            
            
                pickerView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(textLabel).offset(kMargin).constraint
                })
            
            
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 初始化内部组件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
    
        backgroundColor = UIColor.whiteColor()
        
        
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(textLabel)
        addSubview(sourceLabel)
        addSubview(icon)
        addSubview(membershipImage)
        addSubview(verifiedImage)
        addSubview(pickerView)
        
        // 布局界面
        layout()
    }
    
    
    
    // 布局界面
    private func layout() {
    
        // 布局头像图片
        icon.snp_makeConstraints { (make) -> Void in
            
            make.top.left.equalTo(self).offset(kMargin)
            make.width.height.equalTo(kIconHW)
        }
        
        // 布局认证
        verifiedImage.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(icon.snp_trailing).offset(-3)
            make.centerY.equalTo(icon.snp_bottom).offset(-3)
        }
        
        // 布局名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(icon.snp_trailing).offset(kMargin)
            make.top.equalTo(icon.snp_top)
        }
        
        // 布局发表日其
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(icon.snp_bottom)
        }
        
        // 布局来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(kMargin)
            make.top.equalTo(timeLabel)
        }
        
        // 布局text
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(icon)
            make.top.equalTo(icon.snp_bottom).offset(kMargin)
            make.trailing.equalTo(self).offset(-kMargin)
        }
        
        // 布局会员图标
        membershipImage.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(kMargin)
        }
        
        // pickerView布局
        pickerView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textLabel.snp_bottom).offset(kMargin)
            make.leading.equalTo(icon)
            make.width.height.equalTo(100)
        }
        
        // 布局自身高度
        self.snp_makeConstraints { (make) -> Void in
            
           self.bottomConstraint = make.bottom.equalTo(pickerView.snp_bottom).offset(kMargin).constraint
        }
        
    }
    
    // 懒加载nameLabel
    private lazy var nameLabel: UILabel = {
        
        let nameLabel = UILabel()
        
        nameLabel.text = "隔壁老王"
        
        nameLabel.font = UIFont.systemFontOfSize(15)
        
        return nameLabel
    }()
    
    
    // 懒加载发表日期
    private lazy var timeLabel: UILabel = {
        
       let timeLabel = UILabel()
        
        timeLabel.textColor = UIColor.orangeColor()
        
        timeLabel.text = "asdasd"
        
        timeLabel.font = UIFont.systemFontOfSize(12)
        
        return timeLabel
    }()
    
    
    // 懒加载内容text
    private lazy var textLabel: UILabel = {
    
        let textLabel = UILabel()
        
        textLabel.text = "我是内容"
        
        textLabel.numberOfLines = 0
        
        textLabel.font = UIFont.systemFontOfSize(15)
        
        return textLabel
    }()
    
    
    // 懒加载来源
    private lazy var sourceLabel: UILabel = {
    
        let sourceLabel = UILabel()
        
        sourceLabel.font = UIFont.systemFontOfSize(12)
        
        sourceLabel.text = "我是来源"
        
        sourceLabel.textColor = UIColor.darkGrayColor()
        
        return sourceLabel
    }()
    
    // 懒加载pickerView
    private lazy var pickerView: RTStatusPickerView = RTStatusPickerView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
    // 懒加载头像
    private lazy var icon: UIImageView = UIImageView(image: UIImage(named: "avatar_default"))
        
    // 懒加载会员图标
    private lazy var membershipImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
        
    // 懒加载认证
    private lazy var verifiedImage: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
}
